package com.diudiutechnology.www.yadiudiu.yadiudiu.main;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.R;


public class SwipeRefreshListView extends SwipeRefreshLayout implements AbsListView.OnScrollListener {
    private RelativeLayout relativeLayout;
    private ListView mListView;
    private View swipeFooterView;
    private TextView tv;
    private ProgressBar pb;
    private onLoadMoreListener mOnLoadMoreListener;
    private int firstVisibleItem = 0;
    private boolean isRefreshing = false;
    private int mFooterViewHeight;
    public static final String NOMOREDATA = "已无更多数据";
    public static final String LOADING = "正在加载...";
    public static final String LOADFINSH = "加载完成";
    public static final String LOADFAIL = "加载失败";
    public static final String NULLDATA = " ";
    private int color;

    public int getFirstVisibleItem() {
        return firstVisibleItem;
    }

    public void setFirstVisibleItem(int firstVisibleItem) {
        this.firstVisibleItem = firstVisibleItem;
    }

    private static final String TAG = "SwipeRefreshListView";

    public SwipeRefreshListView(Context context) {
        this(context, null);
    }

    public SwipeRefreshListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (mListView == null) {
            findListView();
        }
    }

    /**
     * 初始化ListView的操作必须放在onLayout中，这样才找得到ListView这个子VIew
     */
    private void findListView() {
        int childCount = getChildCount();
        if (childCount > 0) {
            for (int i = 0; i < childCount; i++) {
                View chileView = this.getChildAt(i);
                if (chileView instanceof ListView) {
                    mListView = (ListView) chileView;
                    mListView.setOnScrollListener(this);
                    initFooterView();
                }
            }
        }
    }

    public ListView getListView() {
        return mListView;
    }

    /**
     * 初始化上拉刷新布局
     */
    private void initFooterView() {
        swipeFooterView = View.inflate(getContext(), R.layout.swipe_footerview, null);
        tv = (TextView) swipeFooterView.findViewById(R.id.tv_swipe_msg);
        pb = (ProgressBar) swipeFooterView.findViewById(R.id.pb_swipe);
        relativeLayout = (RelativeLayout) swipeFooterView.findViewById(R.id.rl_swipe);
        relativeLayout.setBackgroundColor(color);
        swipeFooterView.measure(0, 0);
        mFooterViewHeight = swipeFooterView.getMeasuredHeight();
        if (mListView.getFooterViewsCount() == 0) {
            mListView.addFooterView(swipeFooterView, null, false);
            mListView.setFooterDividersEnabled(false);
            mListView.setHeaderDividersEnabled(false);
        }
        swipeFooterView.setPadding(0, -mFooterViewHeight, 0, 0);
    }

    public void setFootViewBackgroundColor(int color) {
        this.color = color;
    }

    @Override
    public void onScrollStateChanged(AbsListView absListView, int scrollState) {
        if (scrollState == SCROLL_STATE_IDLE && mListView.getLastVisiblePosition() + 1 == mListView.getCount()
                && null != mOnLoadMoreListener && !isRefreshing) {
            isRefreshing = true;
            dataLoadEnd(true, LOADING);
            mOnLoadMoreListener.loadMore();
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

    }

    public void setLinstener(boolean bl) {
        if (bl) {
            if (swipeFooterView != null) {
                pb.setVisibility(View.VISIBLE);
                this.swipeFooterView.setPadding(0, 0, 0, 0);
            }
        } else {
            if (swipeFooterView != null && tv != null) {
                isRefreshing = false;
                pb.setVisibility(View.GONE);
                if (TextUtils.equals(tv.getText().toString(), NULLDATA)) {
                    swipeFooterView.setPadding(0, -mFooterViewHeight, 0, 0);
                }
            }
        }
    }

    /**
     * 接口回调
     *
     * @param loadMoreListener
     */
    public void setOnLoadMoreListener(onLoadMoreListener loadMoreListener) {
        this.mOnLoadMoreListener = loadMoreListener;
    }

    /**
     * 加载更多接口
     */
    public interface onLoadMoreListener {
        void loadMore();
    }

    /**
     * 全部数据加载完
     */
    public void dataLoadEnd(boolean b, String str) {
        if (tv != null && !TextUtils.isEmpty(str)) {
            tv.setText(str);
        }
        setLinstener(b);
    }

}